package com.adcc.monitoragent.show.bean;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentSkipListMap;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.adcc.monitoragent.bean.ServiceBean;
import com.adcc.monitoragent.bean.ServiceLogBean;
import com.adcc.monitoragent.show.conf.MonitorRead;
import com.adcc.monitoragent.show.service.MessageService;
import com.adcc.monitoragent.util.ProtertiesUtil;

/**
 * 监控实体类
 * @author charlie
 *
 */
public class WatchBean  implements Serializable {
	private final static Logger log = LoggerFactory
			.getLogger(WatchBean.class);
	
	private static final long serialVersionUID = 1L;
	/**
	 * 集群
	 */
	private static Map<String, ClusterBean> clusterListMap = new ConcurrentSkipListMap<String, ClusterBean>();;
	
	/**
	 * 日志
	 */
	private static  Map<String, LogBean> logListMap = new ConcurrentSkipListMap<String, LogBean>();
	/**
	 * 部署集群
	 */
	private static Map<String, ClusterDeployBean> clusterDeployListMap = new ConcurrentSkipListMap<String, ClusterDeployBean>();
	/**
	 * 监控集群数
	 */
	private static int watchNum = 1;
	/**
	 * 感兴趣的mq队列信息
	 */
	private static Map<String, String> interestMqMetrics = new ConcurrentHashMap<String, String>();
	
	public static Map<String, ClusterBean> getClusterListMap() {
		return clusterListMap;
	}


	public static Map<String, LogBean> getLogListMap() {
		return logListMap;
	}


	public static Map<String, ClusterDeployBean> getClusterDeployListMap() {
		return clusterDeployListMap;
	}


	public static int getWatchNum() {
		return watchNum;
	}
	
	public static Map<String, String> getInterestMqMetrics() {
		return interestMqMetrics;
	}

	/**
	 * 通过提取设置ProtertiesUtil参数
	 */
	public static void initWatch() {
		initWatch(ProtertiesUtil.getShowClusterNames(), ProtertiesUtil.getRecivePort(),
				ProtertiesUtil.getInterestMqMetrics(),ProtertiesUtil.getMaxMqQueueNumber(),
				ProtertiesUtil.getShowLogPathParent(), ProtertiesUtil.getShowLogPaths(),
				ProtertiesUtil.isShowDynamic(), ProtertiesUtil.getInitNodeConfig());
	}

	/**
	 * 
	 * @param watchNum  监控的集群个数
	 * @param interestMqMetrics   关心的mq队列名称
	 * @param logFiles   日志配置
	 */
	/**
	 * 初始化监控
	 * @param clusterNames  监控集群名称
	 * @param recivePort  接收udp端口
	 * @param interestMqMetrics  关心的mq队列名称
	 * @param maxMqQueueNumber  每个队列最大堆报
	 * @param logFileParent  日志跟路径
	 * @param logFiles  日志子路径列表
	 * @param showDynamic 动态生成节点
	 * @param initNodeConfig 初始化节点配置
	 */
	public static void initWatch(List<Object> clusterNames, int recivePort, List<Object> interestMqMetrics, int maxMqQueueNumber, 
			String logFileParent, List<Object> logFiles, boolean showDynamic, String initNodeConfig) {
		WatchBean.watchNum = clusterNames.size();
		for (Object name : clusterNames) {
			clusterListMap.put(name.toString(), new ClusterBean());
			logListMap.put(name.toString(), new LogBean());
			clusterDeployListMap.put(name.toString(), new ClusterDeployBean());
		}
		ProtertiesUtil.setShowClusterNames(clusterNames);
		ProtertiesUtil.setRecivePort(recivePort);
		ProtertiesUtil.setInterestMqMetrics(interestMqMetrics);
		ProtertiesUtil.setMaxMqQueueNumber(maxMqQueueNumber);
		ProtertiesUtil.setShowLogPathParent(logFileParent);
		ProtertiesUtil.setShowLogPaths(logFiles);
		ProtertiesUtil.setShowDynamic(showDynamic);
		ProtertiesUtil.setInitNodeConfig(initNodeConfig);
		for (Object object : interestMqMetrics) {
			try {
				String s[] = object.toString().split(":");
				WatchBean.interestMqMetrics.put(s[0], object.toString());
			} catch (Exception e) {
				log.error("mq配置初始化异常:" + e.getMessage());
			}
		}
		MonitorRead.initWatchBean(initNodeConfig);
	}
	/**
	 * 获取第一个集群监控
	 * @return
	 */
	public static ClusterBean getClusterOne() {
		return clusterListMap.get(ProtertiesUtil.getShowClusterNames().get(0));
	}
	/**
	 * 获取第一个日志
	 * @param start
	 * @param length
	 * @return
	 */
	public static List<ServiceLogBean> getLogOne(int start, int length) {
		List<ServiceLogBean> logs = logListMap.get(ProtertiesUtil.getShowClusterNames().get(0)).getServiceLogList();
		List<ServiceLogBean> list = new ArrayList<ServiceLogBean>();
		int size = logs.size() - 1;
		for (int i = 0; i < length; i++) {
			if(start + i <= size) {
				list.add(logs.get(start + i));
			}
		}
		return list;
	}
	
	/**
	 * 第一个日志大小
	 * @return
	 */
	public static int getLogOneSize() {
		List<ServiceLogBean> logs = logListMap.get(ProtertiesUtil.getShowClusterNames().get(0)).getServiceLogList();
		return logs.size();
	}
	
	
	/**
	 * 获取第一个部署集群监控
	 * @return
	 */
	public static ClusterDeployBean getClusterDeployOne() {
		return clusterDeployListMap.get(ProtertiesUtil.getShowClusterNames().get(0));
	}

	/**
	 * 添加日志
	 * @param logBean
	 */
	public static void addLog(ServiceLogBean logBean) {
		MessageService.watchMsg(ProtertiesUtil.getShowClusterNames().get(0).toString(), logBean);
	}
	/**
	 * 获取服务Map
	 * @param serviceName 服务名
	 * @return
	 */
	public static Map<String, ServiceBean> getServiceMap(String serviceName) {
		Map<String, ServiceBean> gwMap = new HashMap<String, ServiceBean>();
		try {
			gwMap = WatchBean.getClusterOne().
				getServiceGroupMap().get(serviceName).getServiceMap();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return gwMap;
	}
}
